package model

import (
	"context"

	"github.com/Masterminds/squirrel"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ AwProductsModel = (*customAwProductsModel)(nil)

type (
	// AwProductsModel is an interface to be customized, add more methods here,
	// and implement the added methods in customAwProductsModel.
	AwProductsModel interface {
		awProductsModel
		withSession(session sqlx.Session) AwProductsModel
		ListByPage(context context.Context,  session sqlx.Session, pageSize,pageNum int64)([]*AwProducts,error)
	}

	customAwProductsModel struct {
		*defaultAwProductsModel
	}
)

// NewAwProductsModel returns a model for the database table.
func NewAwProductsModel(conn sqlx.SqlConn) AwProductsModel {
	return &customAwProductsModel{
		defaultAwProductsModel: newAwProductsModel(conn),
	}
}

func (m *customAwProductsModel) withSession(session sqlx.Session) AwProductsModel {
	return NewAwProductsModel(sqlx.NewSqlConnFromSession(session))
}

func (m *customAwProductsModel) ListByPage(context context.Context,  session sqlx.Session, pageSize,pageNum int64)([]*AwProducts,error){
	sb := squirrel.Select("*").From(m.table)

	sb = sb.Offset(uint64((pageNum-1)*pageSize)).Limit(uint64(pageSize))

	query,args,err := sb.ToSql()
	if err != nil {
		return nil,err
	}
	var products []*AwProducts
	if session != nil {
		err = session.QueryRowsPartialCtx(context,&products,query,args...)
	}else{
		err = m.conn.QueryRowsPartialCtx(context,&products,query,args...)
	}
	return products,err
}
